package com.example.blog.until;

import com.luciad.imageio.webp.WebPReadParam;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : LiuWenHao
 * @create 2023/3/7 13:26
 */

@Component
public class updown {

    public  void webpToPng(String webpPath, String pngPath) throws IOException {
        // Obtain a WebP ImageReader instance
        ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();

        // Configure decoding parameters
        WebPReadParam readParam = new WebPReadParam();
        readParam.setBypassFiltering(true);

        // Configure the input on the ImageReader
        reader.setInput(new FileImageInputStream(new File(webpPath)));

        // Decode the image
        BufferedImage image = reader.read(0, readParam);

        // the `png` can use `jpg`
        ImageIO.write(image, "png", new File(pngPath));
    }


}
